package exp;

import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;


public class HelloScheduler {

    @Test
    public void test1() throws SchedulerException {
        Date date = new Date();
        System.out.println(new SimpleDateFormat("YYYY/MM/dd HH:ss:mm").format(date));
        System.out.println(new SimpleDateFormat("E").format(date));
        date.setTime(date.getTime() + 3 * 1000);
        JobBuilder job = JobBuilder.newJob(HelloJob.class);
        JobDetail jobDetail = job.withIdentity("MyHelloJob", "admin")
                .usingJobData("message","HelloMyMessage")
                .usingJobData("floatMsg", 0.01F)
                .build();
        //System.out.println("Name:" + jobDetail.getKey().getName());
        //System.out.println("Group:" + jobDetail.getKey().getGroup());
        //System.out.println("Class:" + jobDetail.getKey().getClass());
        //创建trigger实例，定义立即执行
        Date endDate = new Date();
        endDate.setTime(endDate.getTime() + 6 * 1000);
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "admin")
                .usingJobData("helloMyTrigger","helloMyTrigger" + 2.0D)
                //.startNow()
                .startAt(date)
                .endAt(endDate)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(
                        SimpleTrigger.DEFAULT_PRIORITY
                ))
                .build();

        //创建schedule实例
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();

        Scheduler scheduler = schedulerFactory.getScheduler();
        System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:ss:mm").format(new Date()));
        scheduler.start();
        scheduler.scheduleJob(jobDetail,trigger);

    }
}
